package com.bigkoo.convenientbanner.recyler;

import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ly on 2016/10/14.
 */
public class Recyler implements IRecyler {

    private List<View> idleViews;

    public Recyler() {
        idleViews=new ArrayList<>();
    }

    @Override
    public View getIdleView() {
        if(idleViews.isEmpty()) {
            return null;
        }
        return addOrRemoveViews(null);
    }

    @Override
    public void AddToIdelViews(View view) {
        addOrRemoveViews(view);
    }

    private synchronized View addOrRemoveViews(View v){
        if(v!=null && !idleViews.contains(v)){
            idleViews.add(v);
        }else{
            if(idleViews.isEmpty()){
                return null;
            }
            v=idleViews.remove(0);
        }
        return v;
    }
}
